package zb.blog.base.sessionScope;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import zb.blog.base.global.GlobalVar;
import zb.blog.bean.AuthPersonBean;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: BinZhang
 * @Date: 2018/5/11 23:09
 * @Description:session域
 */

public class SessionScope {
    public static HttpServletRequest request;

    /**
     * @Author: BinZhang
     * @Date: 2018/5/11 23:36
     * @Description:从session中获取登录人信息
     */
    public static AuthPersonBean getLoginAuth() {
        Object loginUser = getHttpServletRequest().getSession().getAttribute(GlobalVar.LOGIN_USER);
        if (loginUser != null) {
            return (AuthPersonBean) loginUser;
        }
        return null;
    }

    public static boolean removeUser() {
        if (getLoginAuth() != null) {
            getHttpServletRequest().getSession().removeAttribute(GlobalVar.LOGIN_USER);
            return true;
        }
        return false;
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/5/11 23:34
     * @Description:获取request域
     */
    public static HttpServletRequest getHttpServletRequest() {
        request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    /**
     * @Author: BinZhang
     * @Date: 2018/5/19 0:09
     * @Description:获取网站域名+端口号
     */
    public static String getWebSit() {
        return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/";
    }
}
